package com.nononsenseapps.feeder.ui.common.htmlview.converter

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Point
import android.graphics.drawable.Drawable
import com.bumptech.glide.request.target.Target
import com.nononsenseapps.feeder.ui.R
import com.nononsenseapps.feeder.ui.common.htmlview.SensibleSpannableStringBuilder
import com.nononsenseapps.feeder.glide.GlideImageLoader
import com.nononsenseapps.feeder.util.PrefUtils
import org.ccil.cowan.tagsoup.Parser
import java.net.URL

class GlideConverter(context: Context,
                     source: String,
                     siteUrl: URL,
                     parser: Parser,
                     maxSize: Point,
                     allowDownload: Boolean,
                     imageCount:Int = 0,
                     spannableStringBuilder:  SensibleSpannableStringBuilder = SensibleSpannableStringBuilder()) :
        AbstractLoadImageHtmlConverter(context,source, siteUrl, parser, maxSize,allowDownload,imageCount, spannableStringBuilder) {


    override fun loadImageAsBitmap(urlLink: String, width: Int, height: Int): Bitmap {
       return GlideImageLoader.glideAsBitmap(context, urlLink, allowDownload).fitCenter().submit(width, height).get()
    }

    override fun loadImageAsDrawable(urlLink: String, width: Int, height: Int): Drawable {
        return GlideImageLoader.glide(context, urlLink, allowDownload)
                .placeholder(when (PrefUtils.isNightMode(context)) {
                    true -> R.drawable.placeholder_image_article_night
                    false -> R.drawable.placeholder_image_article_day
                })
                .error(when (PrefUtils.isNightMode(context)) {
                    true -> R.drawable.placeholder_image_article_night
                    false -> R.drawable.placeholder_image_article_day
                })
                .submit().get()
    }

    override fun loadImageAsGif(urlLink: String, width: Int, height: Int): Drawable {
        return GlideImageLoader.glideAsGif(context, urlLink, allowDownload)
                .placeholder(when (PrefUtils.isNightMode(context)) {
                             true -> R.drawable.placeholder_image_article_night
                              false -> R.drawable.placeholder_image_article_day
                             })
                .error(when (PrefUtils.isNightMode(context)) {
                    true -> R.drawable.placeholder_image_article_night
                    false -> R.drawable.placeholder_image_article_day
                })
                .submit().get()
    }

    private fun loadImageAsGifInto(urlLink: String, width: Int, height: Int, target: Target<Drawable>) {
        GlideImageLoader.glide(context, urlLink, allowDownload)
                .placeholder(when (PrefUtils.isNightMode(context)) {
                    true -> R.drawable.placeholder_image_article_night
                    false -> R.drawable.placeholder_image_article_day
                })
                .error(when (PrefUtils.isNightMode(context)) {
                    true -> R.drawable.placeholder_image_article_night
                    false -> R.drawable.placeholder_image_article_day
                })
                .into(target)
    }

    override fun startGifImg(text:  SensibleSpannableStringBuilder, thumbnail: Drawable, len: Int, imgLink: String, width: Int, height: Int) {
        thumbnail.let {
            /*val proxyDrawable = ProxyDrawable()
            loadImageAsGifInto(imgLink,width,height, DrawableTarget(proxyDrawable, width, height))
            val spannable = object: ClickableImageSpan(proxyDrawable,imgLink){

            }
            Timber.d("loading gif image with glide url:$imgLink,src width:${it.bounds.width()} \t width:${proxyDrawable.bounds.width()} \t height:${proxyDrawable.bounds.height()}")
            text.setSpan(spannable, len, text.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)*/
        }
    }
}

